import { profile } from "../../../../js/modules/store.js";
import { api, util} from '../../../../js/util';
import { router } from '../../../../js/router';
import authorize from '../../../../js/modules/authorize.js'
import {config} from "../../../../js/modules/gloab";
import BasePage from '../../../../js/common/base-page'
import { chooseLocation } from "../../../../js/authSetting.js";
import { getStorageSync, setStorageSync } from "../../../../js/compatible.js";
import { isEmpty } from "../../../../js/newUtil.js";
var app =getApp();
BasePage({
  pageKey:"setting",

  /**
   * 页面的初始数据
   */
  data: {
    isIphoneX:app.globalData.isIphoneX,
    LocationAddress:'',
    forms: {
      Name: {
        value: "",
        validate: "isnull",
        error: "请输入公司名称"
      },
      Responsible: {
        value: "",
        validate: "isnull",
        error: "请输入联系人名称"
      },
      Tel: {
        value: "",
        validate: "isnull",
        error: "请输入电话号码"
      },
      Address: {
        value: "",
        validate: "isnull",
        error: "请输入联系地址"
      },
      Latitude: {
        value: 0
      },
      Longitude: {
        value:0
      },
      Province: {
        value: ""
      },
      City: {
        value: ""
      },
      Area: {
        value: ""
      },
      Town: {
        value: ""
      },
    },
    shop:null,
    DeviceNo:'',
    allowModifyName:false//是否允许自主修改客户名称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var entity=authorize.getEntity();
    this.setData({
      DeviceNo: getStorageSync('openid')
    })
    this.data.forms.Name.value = entity.role.Name;
    this.data.forms.Responsible.value = entity.role.Responsible;
    this.data.forms.Tel.value = entity.role.Tel;
    this.data.forms.Address.value = entity.role.Address;
    this.data.forms.Latitude.value = entity.role.Latitude;
    this.data.forms.Longitude.value = entity.role.Longitude;
    this.data.forms.Province.value = entity.role.Province;
    this.data.forms.City.value = entity.role.City;
    this.data.forms.Area.value = entity.role.Area;
    this.data.forms.Town.value = entity.role.Town;
    this.setData({
      forms:this.data.forms,
      shop:entity.role
    });
    this.getData()
    this.getWholesaleConfig();
  },
  showPicker () {
    let that = this
    let picker = this.selectComponent('#pickerStreet')
    let forms = this.data.forms || {}
    picker.$$showComponent({
      Id: '',
      success (res) {
        console.log('选择的省市区街数据', res)
        try {
          forms.Province.value = res.province.Name || ''
          forms.City.value = res.city.Name || ''
          forms.Area.value = res.district.Name || ''
          if (res.street) {
            forms.Town.value = res.street.Name || ''
          }
          that.setData({
            forms
          })
        } catch (error) {
          
        }
      }
    })
  },
  /**
   * 获取最新的用户信息
   */
  getData () {
    let that = this
    let dns = app.dns || {}
    let forms = this.data.forms || {}
    if (isEmpty(dns)) {
      return
    }
    var data = {
      proprietor: dns.proprietor || '',
      proprietorId: dns.proprietorId || '',
    }
    config.getAvatar(data, function (res) {
      console.log(res)
      if (res.Success && !isEmpty(res.Content)) {
        let userInfo = res.Content
        forms.Name.value = userInfo.Name;
        forms.Responsible.value = userInfo.Responsible;
        forms.Tel.value = userInfo.Tel;
        forms.Address.value = userInfo.Address;
        forms.Latitude.value = userInfo.Latitude;
        forms.Longitude.value = userInfo.Longitude;
        forms.Province.value = userInfo.Province;
        forms.City.value = userInfo.City;
        forms.Area.value = userInfo.Area;
        forms.Town.value = userInfo.Town;
        that.setData({
          forms
        })
      }
    })
  },
  /**加载商品类的配置项参数 */
  getWholesaleConfig() {
    var that = this;
    var storeInfo = authorize.getEntity();
    var fkid = app.dns.proprietorId;
    var fkflag = app.dns.proprietor;
    config.getWholesaleConfigs(
    fkid,
    fkflag,
    "Wholesale",
    function (model) {
      that.setData({
        allowModifyName: storeInfo.manager.Initiator == 1 && model.AllowModifyName == true
      });
    });
  },
  /**
   * 表单值绑定
   */
  bindFormValue: function (e) {
    this.data.forms[e.target.id].value = e.detail.value;

  },
  /**
   * 
   */
  onSubmitEvent: function (e) {
    profile.save_setting(this.data.forms);

  },
  /**
   * 取消
   */
  onCancelEvent: function (e) {
    setStorageSync('tabindex', app.dns.centerNum);
    router.switchTab({
      path: "center"
    });
  },
  getLocation(){
    app.getCurrentLocation(api.amapKey,(res) => {
      if(res){
        this.data.forms.Latitude.value = res.lat;
        this.data.forms.Longitude.value = res.lng;
        this.data.forms.Province.value = res.province;
        this.data.forms.City.value = res.city;
        this.data.forms.Area.value = res.district;
        this.data.forms.Town.value = res.town;
        this.data.forms.Address.value = res.detailAddress;
        this.setData({
          forms:this.data.forms
        })
      }
    })
  },
  /**
   * 定位 
   * */
  onSetLocationEvent: function () {
    var that = this
    chooseLocation({
      success: function (res) {
        // success
        var address = res.address;
        var proExp = ".+[省]", cityExp = "^.*?[市]", disExp1 = ".+[区]",disExp2 = ".+[县]",disExp3 = ".+[市]";
        var province = address.match(new RegExp(proExp)); // 省
        var city = address.match(new RegExp(cityExp)); // 市
        var district1 = address.match(new RegExp(disExp1)); //区
        var district2 = address.match(new RegExp(disExp2)); //县
        var district3 = address.match(new RegExp(disExp3)); //县级市
        if (province==null){
          that.data.forms.Province.value="";
          that.data.forms.City.value = city;
          if (district1 != null) {
            district1 = district1[0].replace(city, ""); //区
            that.data.forms.Area.value = district1;
          }
          else if(district2 != null){
            district2 = district2[0].replace(city, "");  //县
            that.data.forms.Area.value = district2;
          }else{
            district3 = district3[0].replace(city, "");  //县级市
            that.data.forms.Area.value = district3;
          }
        }
        else{
          that.data.forms.Province.value = province;
          city = city[0].replace(province, ""); // 市
          that.data.forms.City.value = city;
          if (district1 != null) {
            district1 = district1[0].replace(province, "").replace(city, ""); //区
            that.data.forms.Area.value = district1;
          }
          else if(district2 != null){
            district2 = district2[0].replace(province, "").replace(city, "");//县
            that.data.forms.Area.value = district2;
          }else{
            district3 = district3[0].replace(province, "").replace(city, "");//县级市
            that.data.forms.Area.value = district3;
          }
        }
        var str = that.data.forms.Province.value + that.data.forms.City.value +that.data.forms.Area.value;
        var detail = address.replace(str,"");
        that.data.LocationAddress=res.address;
        that.data.forms.Address.value =detail+" "+res.name;
        that.data.forms.Latitude.value = res.latitude;
        that.data.forms.Longitude.value = res.longitude;
        that.data.forms.Town.value = ''
        that.setData({
            LocationAddress: res.address,
            forms:that.data.forms
        })
      },
      fail: function () {
        util.alert("定位失败");
      },
      complete:function(){
        console.log("定位结束")
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})